package com.youtui.manager.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.common.util.NextIdUtil;
import com.youtui.manager.mapper.SmsCodeMapper;
import com.youtui.manager.model.SmsCode;
import com.youtui.manager.model.SmsCodeExample;
import com.youtui.manager.model.SmsCodeExample.Criteria;
import com.youtui.manager.service.ISmsCodeService;

@Service
public class SmsCodeServiceImpl extends BaseServiceImpl implements ISmsCodeService {

	@Autowired
	SmsCodeMapper smsCodeMapper;

	@Override
	public String insertSmsCode(String mobile, int type) {

		if (StringUtils.isBlank(mobile) || mobile.length() != 11) {
			return null;
		}

		String code = NextIdUtil.getRandomNumString(4);
		SmsCode smscode = new SmsCode();
		smscode.setId(NextIdUtil.getNextId());
		smscode.setType(type);
		smscode.setCode(code);
		smscode.setMobile(mobile);
		smscode.setStartTime(new Date());
		smscode.setEndTime(DateUtils.addMinutes(new Date(), 10)); //10分钟有效期
		smsCodeMapper.insert(smscode);

		return code;
	}

	@Override
	public int findCodeByMobile(String mobile, int type) {
		if (StringUtils.isBlank(mobile)) {
			return 0;
		}
		SmsCodeExample example = new SmsCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andMobileEqualTo(mobile);
		criteria.andStartTimeLessThan(new Date());
		criteria.andEndTimeGreaterThan(new Date());
		criteria.andTypeEqualTo(type);
		int count = smsCodeMapper.countByExample(example);
		return count;
	}

	@Override
	public int findCodeSendByMobile(String mobile, int type) {
		if (StringUtils.isBlank(mobile)) {
			return 0;
		}
		Date date = new Date();
		Date date2 = DateUtils.addMinutes(date, -1);
		SmsCodeExample example = new SmsCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andMobileEqualTo(mobile);
		criteria.andStartTimeGreaterThan(date2);
		criteria.andStartTimeLessThan(date);
		criteria.andTypeEqualTo(type);
		int count = smsCodeMapper.countByExample(example);
		return count;
	}

	@Override
	public boolean checkSmsCode(String mobile, String code, int type) {

		SmsCodeExample example = new SmsCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andMobileEqualTo(mobile);
		criteria.andStartTimeLessThan(new Date());
		criteria.andEndTimeGreaterThan(new Date());
		criteria.andTypeEqualTo(type);
		example.setOrderByClause(" id desc ");
		List<SmsCode> list = smsCodeMapper.selectByExample(example);
		if (list == null || list.size() == 0) {
			return false;
		}
		SmsCode smsCode = list.get(0);

		if (code.equals(smsCode.getCode())) {
			return true;
		} else {
			return false;
		}

	}

}
